package com.cpx.controller;

import com.cpx.feign.PanicBuyingFeign;
import com.cpx.vo.JsonResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: PanBin
 * @Date: 2018-9-21 15:13
 * @Description: 秒杀的controller
 * @Version: 1.0
 */
@RestController
public class PanicBuyingController {

    @Autowired
    private PanicBuyingFeign panicBuyingFeign;

    @RequestMapping(value = "/seckill",method = RequestMethod.POST)
    public JsonResultVo seckill(String key) {
        JsonResultVo resultVo = new JsonResultVo();
        // 实现Runnable接口实现多线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 2000; i++) {
                    boolean isSuccess =  panicBuyingFeign.seckill(key);
                    if (isSuccess) {
                        resultVo.setData(i + "--抢购成功");
                        System.err.println(i + "--抢购成功");
                    } else {
                        resultVo.setErrMsg(i + "--抢购失败");
                        System.err.println(i + "--抢购失败");
                    }
                }
            }
        }){}.start();
        return resultVo;
    }
}
